package com.mobile.collect.utils;

import android.app.Activity;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

import java.io.File;

import cn.droidlover.xdroidmvp.kit.NLog;

public class TakePicUtils implements InvokeListener {
    private TakePhoto takePhoto;
    Activity mActivity;
    private InvokeParam invokeParam;
    TakePhoto.TakeResultListener takeResultListener;
//    int size;
//    CropOptions cropOptions;

    public TakePicUtils(Activity mActivity, TakePhoto.TakeResultListener takeResultListener) {
        this.mActivity = mActivity;
        this.takeResultListener = takeResultListener;
        int size = Math.min(mActivity.getResources().getDisplayMetrics().widthPixels, mActivity.getResources().getDisplayMetrics().heightPixels);
        NLog.d("TakePicUtils","size="+size);
//        cropOptions = new CropOptions.Builder().setOutputX(size/2).setOutputY(size/2).setWithOwnCrop(true).create();
        getTakePhoto();
    }

    public TakePhoto getTakePhoto() {
        //获得TakePhoto实例
        if (takePhoto == null) {
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(mActivity, takeResultListener));
        }
        //设置压缩规则，最大500kb
//        takePhoto.onEnableCompress(new CompressConfig.Builder().setMaxSize(1024 * 1024).create(), true);
        return takePhoto;
    }

    public void takePhotoFromCapture() {
        File file = FileUtil.getTmpPicFile(System.currentTimeMillis() + ".png");
        Uri imageUri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            imageUri = FileProvider.getUriForFile(mActivity, mActivity.getPackageName() + ".fileprovider", file);
        } else {
            imageUri = Uri.fromFile(file);
        }

        takePhoto.onPickFromCapture(imageUri);
    }

    public void onPickMultiple(int limit) {
        takePhoto.onPickMultiple(limit);

    }

    public void onPickFromGallery() {
        File file = FileUtil.getTmpPicFile(System.currentTimeMillis() + ".png");
        Uri imageUri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            imageUri = FileProvider.getUriForFile(mActivity, mActivity.getPackageName() + ".fileprovider", file);
        } else {
            imageUri = Uri.fromFile(file);
        }
        takePhoto.onPickFromGallery();
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(mActivity), invokeParam.getMethod());
        if (PermissionManager.TPermissionType.WAIT.equals(type)) {
            this.invokeParam = invokeParam;
        }
        return type;
    }

    public InvokeParam getInvokeParam() {
        return invokeParam;
    }


}
